package org.dreamwork.jasmine2.util;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-23
 * Time: 14:23:22
 */
public class StringConverter {
    public static String contvertToString (Object value) {
        if (value == null) return "";
        if (value.getClass () == String.class) return (String) value;
        return value.toString ();
    }

    public static Object convertFromString (Class type, String expression) {
        return null;
    }

    public static boolean stringEquals (Object value1, Object value2) {
        return stringEquals (value1, value2, false);
    }

    public static boolean stringEquals (Object value1, Object value2, boolean ignoreCase) {
        if (value1 == null || value2 == null) return false;
        if (ignoreCase)
            return value1.toString ().equalsIgnoreCase (value2.toString ());
        return value1.toString ().equals (value2.toString ());
    }

    public static boolean isEmpty (String string) {
        return (string == null || string.trim ().length () == 0);
    }

    public static boolean equals (Object value1, Object value2) {
        if (value1 == value2) return true;
        if (value1 == null || value2 == null) return false;
        if (value1.getClass () == value2.getClass ()) return value1.equals (value2);
        String s1 = value1.toString ();
        String s2 = value2.toString ();
        return s1.equals (s2);
    }

    public static boolean contains (Collection collection, Object value) {
        if (collection.contains (value)) return true;

        for (Iterator iter = collection.iterator (); iter.hasNext (); ) {
            Object other = iter.next ();
            if (equals (value, other)) return true;
        }
        
        return false;
    }

    public static String generateUUID () {
        long timestamp = System.currentTimeMillis ();
        long uiid = timestamp ^ (long) (timestamp * Math.random ());
        return String.valueOf (uiid);        
    }
}